package com.yvan.serverless.mybatis;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.collect.Maps;
import com.yvan.Conv;

import java.io.IOException;
import java.util.Map;

public class PageDbSerializer extends JsonSerializer<PageDb> {

    public static final SimpleModule MODEL;

    static {
        MODEL = new SimpleModule();
        MODEL.addSerializer(PageDb.class, new PageDbSerializer());
    }

    @Override
    public void serialize(PageDb pagionation,
                          JsonGenerator gen,
                          SerializerProvider serializers) throws IOException, JsonProcessingException {

        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("total", Conv.asInteger(pagionation.getTotal()));
        map.put("size", Conv.asInteger(pagionation.getSize()));
        map.put("current", Conv.asInteger(pagionation.getCurrent()));

        //    map.put("pageSize", pagionation.getSize());
        //    map.put("pageNo", pagionation.getCurrent());
        //    map.put("next",
        //        pagionation.hasNext() ? pagionation.getCurrent() + 1 : pagionation.getCurrent());
        //    map.put("last", pagionation.getPages());
        //    map.put("first", 1);
        //    map.put("prev",
        //        pagionation.hasPrevious() ? pagionation.getCurrent() - 1 : pagionation.getCurrent());
        //    map.put("count", pagionation.getTotal());
        gen.writeObject(map);

        //gen.writeNumberField("pageSize", pagionation.getSize());
        //gen.writeNumberField("pageNo", pagionation.getCurrent());
        //gen.writeNumberField("next", pagionation.hasNext() ? pagionation.getCurrent() + 1 : pagionation.getCurrent());
        //gen.writeNumberField("last", pagionation.getPages());
        //gen.writeNumberField("first", 1);
        //gen.writeNumberField("prev", pagionation.hasPrevious() ? pagionation.getCurrent() - 1 : pagionation.getCurrent());
        //gen.writeNumberField("count", pagionation.getTotal());
    }
}
